import {message} from "antd"
const infodata={
    data:[],
    code:0
}
const reducer=(state=infodata,{type,payload}:any)=>{
   let newState=JSON.parse(JSON.stringify(state))
   const show_Toast=(code:number,msg:any)=>{
    switch (code) {
      case 200:
        return message.success(payload.msg)
        case 500:
        return message.error(payload.msg)  
        case 201:
          return message.error(payload.msg)
          case 202:
          return message.success(payload.msg)  
      default:
        break;
    }
   }
   switch (type) {
    case "LOGIN":
        if(payload.code===200){
       newState.userarr={
         username:payload.username,
         password:payload.password
       };
       window.localStorage.setItem("token",JSON.stringify({
         username:payload.username,
         password:payload.password
       }))
      
        }
        show_Toast(payload.code,payload.msg)
        newState.code = payload.code;
       return newState
       case "REG":
        if(payload.code===201){
          show_Toast(payload.code,payload.msg)
        }else if(payload.code===202){
          show_Toast(payload.code,payload.msg)
        }
        return newState
    default:
        return newState
   }
}
export default reducer